#include "login.h"
#include "ui_login.h"


Login::Login(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Login)
    ,myDB(new Database("..\\..\\learn_test1_database.db"))
{
    ui->setupUi(this);

    this->myDB->myOpen();
}

Login::~Login()
{
    delete ui;
    delete myDB;//析构的时候会自动关闭数据库
    qDebug() << "登录界面析构";
}

void Login::on_btn_confirm_clicked()
{
    QString name = ui->le_name->text();
    QString password  = ui->le_password->text();

    QSqlQuery query = this->myDB->executeQuery(
        "SELECT * FROM user WHERE customer = ? AND password = ?",{name,password});


    if(query.next())
    {
        qDebug() << "用户名和密码正确，登录成功";
        MainWindow* m = new MainWindow();
        m->show();
        this->close();
    }
    else
    {
        qDebug() << "用户名或者密码错误，登录失败";
        QMessageBox::critical(this,"提示","用户名或者密码错误");
    }
}


void Login::on_btn_exit_clicked()
{
//     done(QDialog::Rejected);
    this->close();
}

